package com.nyist.diseasepreventionserver.util;


import com.sun.mail.util.MailSSLSocketFactory;
import sun.print.BackgroundLookupListener;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class MailUtil {
    private static String sendMail = PropertiesUtil.getProperty("sendMail");
    private static String host = PropertiesUtil.getProperty("host");
    private static String password = PropertiesUtil.getProperty("password");
    private String title;
    private String main;
    private String toMail;

    public MailUtil(String title, String main, String toMail) {
        this.title = title;
        this.main = main;
        this.toMail = toMail;
    }

    public static boolean send(String title,String main,String toMail) throws GeneralSecurityException {
        MailUtil mailUtil = new MailUtil(sendMail,host,password);
        boolean result = mailUtil.sendMail(title,main,toMail);
        return result;
    }
    private static boolean sendMail(String title,String main,String toMail) throws GeneralSecurityException {
        boolean send = false;
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);

        properties.put("mail.smtp.auth", "true");
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sf);
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties,new Authenticator(){
            public PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(sendMail, password); //发件人邮件用户名、授权码
            }
        });

        try{
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(sendMail));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(toMail));

            // Set Subject: 头部头字段
            message.setSubject(title);//标题

            // 设置消息体
            message.setText(main);//内容

            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully");
            send = true;
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
        return send;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMain() {
        return main;
    }

    public void setMain(String main) {
        this.main = main;
    }

    public String getToMail() {
        return toMail;
    }

    public void setToMail(String toMail) {
        this.toMail = toMail;
    }
}
